package com.example.appjetpackcompose

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.ComposeView
import androidx.compose.ui.platform.ViewCompositionStrategy
import androidx.compose.ui.viewinterop.AndroidView
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.scwang.smart.refresh.footer.ClassicsFooter
import com.scwang.smart.refresh.header.ClassicsHeader
import com.scwang.smart.refresh.layout.SmartRefreshLayout
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener
import com.scwang.smart.refresh.layout.listener.OnRefreshListener


class TestActivity4Fragment2: Fragment() {

    val TAG=TestActivity4Fragment2::class.java.simpleName

    companion object{
        fun newInstance(): TestActivity4Fragment2{
            val args = Bundle()
            val fragment = TestActivity4Fragment2()
            fragment.arguments = args
            return fragment
        }
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
//        return super.onCreateView(inflater, container, savedInstanceState)
        return ComposeView(requireContext()).apply {
            setViewCompositionStrategy(ViewCompositionStrategy.DisposeOnViewTreeLifecycleDestroyed)
            setContent {
                MaterialTheme{
                    ContentExample(object :OnRefreshListener{
                        override fun onRefresh(refreshLayout: RefreshLayout) {
                            refreshLayout.finishRefresh(2000)
                        }
                    },object :OnLoadMoreListener{
                        override fun onLoadMore(refreshLayout: RefreshLayout) {
                            refreshLayout.finishLoadMore(2000)
                        }
                    },Modifier.fillMaxSize())
                }
            }
        }
    }

}

@Composable
fun CustomView(onRefreshListener:OnRefreshListener,onLoadMoreListener: OnLoadMoreListener,modifier: Modifier = Modifier) {
    // Adds view to Compose
    AndroidView(
        modifier = Modifier.fillMaxSize(), // Occupy the max size in the Compose UI tree
        factory = { context ->
            // Creates custom view
            SmartRefreshLayout(context).apply {
                var recyclerView=RecyclerView(context)
                recyclerView.layoutManager=LinearLayoutManager(context,LinearLayoutManager.VERTICAL,false)
                addView(recyclerView)
                setRefreshHeader(ClassicsHeader(context))
                setRefreshFooter(ClassicsFooter(context))
            }
        },
        update = { view ->
            // View's been inflated or state read in this block has been updated
            // Add logic here if necessary

            // As selectedItem is read here, AndroidView will recompose
            // whenever the state changes
            // Example of Compose -> View communication
            view.setOnRefreshListener(onRefreshListener)
            view.setOnLoadMoreListener(onLoadMoreListener)
        }
    )
}

@Composable
fun ContentExample(onRefreshListener:OnRefreshListener,onLoadMoreListener: OnLoadMoreListener,modifier: Modifier = Modifier) {
    Column(Modifier.fillMaxSize()) {
        Text("Look at this CustomView!")
        CustomView(onRefreshListener,onLoadMoreListener,modifier)
    }
}